package com.github.nuggets.controller;

import com.github.nuggets.model.param.AccountUpdateParams;
import com.github.nuggets.model.param.PasswordUpdateParams;
import com.github.nuggets.model.pojo.LoginAccountInfo;
import com.github.nuggets.model.result.BaseResult;
import com.github.nuggets.model.vo.NuggetsAccountDetailVO;
import com.github.nuggets.service.AccountManagerService;
import com.github.nuggets.util.LoginAccountUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 登录用户的管理接口
 * @author: wangsj
 * @date: 2022/3/18 2:40 下午
 */
@RestController
@RequestMapping("/nuggets")
public class LoginAccountManagerController {

    @Autowired
    AccountManagerService accountManagerService;

    @Operation(summary = "获取登录用户", description = "获取登录用户详情信息")
    @GetMapping("/account")
    public BaseResult<NuggetsAccountDetailVO> getLoginAccount(OAuth2Authentication auth) {

        return BaseResult.success(accountManagerService.findLoginAccountDetails(auth));
    }

    @Operation(summary = "更新用户信息", description = "更新用户信息")
    @PostMapping("/account")
    public BaseResult<Boolean> updaateAccount(OAuth2Authentication auth, @RequestBody AccountUpdateParams updatePrams){
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(accountManagerService.updateAccount(loginAccount.getId(),updatePrams));
    }

    @Operation(summary = "注销用户", description = "注销用户")
    @DeleteMapping("/account")
    public BaseResult<Boolean> removeAccount(OAuth2Authentication auth){
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(accountManagerService.removeAccount(loginAccount.getId()));
    }

    @Operation(summary = "更新用户密码", description = "更新用户密码")
    @PostMapping("/account/password")
    public BaseResult<Boolean> updaatePassword(OAuth2Authentication auth, @RequestBody PasswordUpdateParams updatePrams){
        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);
        return BaseResult.success(accountManagerService.updatePassword(loginAccount.getId(),updatePrams));
    }


}
